package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.CoFeedback;
import com.ruoyi.system.service.ICoFeedbackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Date;
import java.util.Map;

/**
 * 反馈信息
 */
@RestController
@RequestMapping("/system/feedback")
public class CoFeedbackController extends BaseController {
    @Autowired
    private ICoFeedbackService feedbackService;

    /**
     * 获取反馈列表
     */
    @PreAuthorize("@ss.hasPermi('system:feedback:list')")
    @GetMapping("/list")
    public TableDataInfo list(CoFeedback feedback) {
        startPage();
        List<CoFeedback> list = feedbackService.selectFeedbackList(feedback);
        return getDataTable(list);
    }

    /**
     * 获取用户的反馈列表
     */
    @PreAuthorize("@ss.hasPermi('system:feedback:list')")
    @GetMapping("/user/{userId}")
    public TableDataInfo listByUser(@PathVariable("userId") Long userId) {
        startPage();
        List<CoFeedback> list = feedbackService.selectFeedbacksByUserId(userId);
        return getDataTable(list);
    }

    /**
     * 获取合同的反馈列表
     */
    @PreAuthorize("@ss.hasPermi('system:feedback:list')")
    @GetMapping("/contract/{contractId}")
    public TableDataInfo listByContract(@PathVariable("contractId") Long contractId) {
        startPage();
        List<CoFeedback> list = feedbackService.selectFeedbacksByContractId(contractId);
        return getDataTable(list);
    }

    /**
     * 获取反馈详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:feedback:query')")
    @GetMapping(value = "/{feedbackId}")
    public AjaxResult getInfo(@PathVariable("feedbackId") Long feedbackId) {
        return success(feedbackService.selectFeedbackById(feedbackId));
    }

    /**
     * 新增反馈
     */
    @PreAuthorize("@ss.hasPermi('system:feedback:add')")
    @Log(title = "反馈管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody CoFeedback feedback) {
        feedback.setUserId(SecurityUtils.getUserId());
        feedback.setStatus(0); // 设置初始状态为未处理
        return toAjax(feedbackService.insertFeedback(feedback));
    }

    /**
     * 修改反馈
     */
    @PreAuthorize("@ss.hasPermi('system:feedback:edit')")
    @Log(title = "反馈管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody CoFeedback feedback) {
        return toAjax(feedbackService.updateFeedback(feedback));
    }

    /**
     * 删除反馈
     */
    @PreAuthorize("@ss.hasPermi('system:feedback:remove')")
    @Log(title = "反馈管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{feedbackIds}")
    public AjaxResult remove(@PathVariable Long[] feedbackIds) {
        return toAjax(feedbackService.deleteFeedbackByIds(feedbackIds));
    }

    /**
     * 更新反馈状态
     */
    @PreAuthorize("@ss.hasPermi('system:feedback:edit')")
    @Log(title = "反馈管理", businessType = BusinessType.UPDATE)
    @PutMapping("/status/{feedbackId}/{status}")
    public AjaxResult updateStatus(@PathVariable("feedbackId") Long feedbackId, @PathVariable("status") Integer status) {
        return toAjax(feedbackService.updateFeedbackStatus(feedbackId, status));
    }

    /**
     * 回复反馈
     */
    @PreAuthorize("@ss.hasPermi('system:feedback:reply')")
    @Log(title = "反馈管理", businessType = BusinessType.UPDATE)
    @PutMapping("/reply/{feedbackId}")
    public AjaxResult reply(@PathVariable("feedbackId") Long feedbackId, @RequestBody String reply) {
        CoFeedback feedback = new CoFeedback();
        feedback.setId(feedbackId);
        feedback.setReply(reply);
        feedback.setReplyTime(new Date());
        feedback.setStatus(2); // 设置状态为已处理
        return toAjax(feedbackService.updateFeedback(feedback));
    }

    /**
     * 获取反馈统计信息
     */
    @PreAuthorize("@ss.hasPermi('system:feedback:list')")
    @GetMapping("/stats")
    public AjaxResult getStats()
    {
        Map<String, Object> stats = feedbackService.selectFeedbackStats();
        return success(stats);
    }
}
